
package com.hkfn.rfb.common.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.LinkedList;
import java.util.List;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    public static final int TYPE_FOOTER = -1;
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private LinkedList<T> mDatas = new LinkedList<T>();
    private View mHeaderView;
    private View mFooterView;
    private OnItemClickListener<T> mListener;
    private OnItemLongClickListener<T> onItemLongClickListener;
    private boolean needFooter = true;

    public boolean isNeedFooter() {
        return needFooter;
    }

    public void setNeedFooter(boolean needFooter) {
        this.needFooter = needFooter;
    }

    public void setOnItemClickListener(OnItemClickListener<T> li) {
      mListener = li;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener<T> onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setHeaderView(View headerView) {
      mHeaderView = headerView;
      notifyItemInserted(0);
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount() - 1);
      }

    public LinkedList<T> getmDatas() {
        return mDatas;
    }

    public View getHeaderView() {
      return mHeaderView;
    }

    public View getFooterView() {
        return mFooterView;
      }

    public void addDatas(List<T> datas) {
        if (!this.mDatas.containsAll(datas) && datas != null && datas.size() > 0) {
            for (T modle : datas) {
                if (!this.mDatas.contains(modle)) {
                    this.mDatas.add(modle);
                }
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 不去重
     * @param datas
     */
    public void addAllDatas(List<T> datas) {
        if (datas != null && datas.size() > 0) {
            this.mDatas.addAll(datas);
            notifyDataSetChanged();
        }
    }

    public void refreshAllDatas(List<T> datas) {
        this.mDatas.clear();
        if (datas != null) {
            this.mDatas = new LinkedList<T>(datas);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        this.mDatas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (null == mHeaderView && null == mFooterView) {
            return getItemOtherViewType(position);
        }

        if (null != mFooterView) {
            if (position == (getItemCount() - 1)) {
                return TYPE_FOOTER;
            }
        }

        if (null != mHeaderView) {
            if (position == 0) {
                return TYPE_HEADER;
            } else {
                position -= 1;
            }
        }

        return getItemOtherViewType(position);
    }

    protected int getItemOtherViewType(int position) {
        return TYPE_NORMAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return new Holder(mHeaderView);
        }

        if (mFooterView != null && viewType == TYPE_FOOTER) {
            return new Holder(mFooterView);
        }
        return onCreate(parent, viewType);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            return;
        }
        if (getItemViewType(position) == TYPE_FOOTER) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
        final T data = mDatas.get(pos);
        onBind(viewHolder, pos, data);
        if (mListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v, pos, data);
                }
            });
        }
        if (null != onItemLongClickListener) {
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickListener.onItemLongClick(v, pos, data);
                    return false;
                }
            });
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
      super.onAttachedToRecyclerView(recyclerView);
      RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
      if(manager instanceof GridLayoutManager) {
        final GridLayoutManager gridManager = ((GridLayoutManager) manager);
        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
          @Override
          public int getSpanSize(int position) {
              int spanSize = -1;
              switch (getItemViewType(position)) {
              case TYPE_HEADER:
              case TYPE_FOOTER:
                  spanSize = gridManager.getSpanCount();
                break;
              case TYPE_NORMAL:
                  spanSize = 1;
                  break;
              default:
                  spanSize = 1;
                break;
            }
            return spanSize;
          }
        });
      }
    }

    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
      super.onViewAttachedToWindow(holder);
      ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
      if(lp != null
          && lp instanceof StaggeredGridLayoutManager.LayoutParams
          && holder.getPosition() == 0) {
        StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
        p.setFullSpan(true);
      }
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        // TODO Auto-generated method stub

        super.onViewRecycled(holder);
    }

    public int getRealPosition(ViewHolder holder) {
      int position = holder.getPosition();
      return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        int size = getSubClassRealSize();
        if (null == mFooterView) {
            return mHeaderView == null ? size : size + 1;
        }

        if (null == mHeaderView) {
            return mFooterView == null ? size : size + 1;
        }

        return size + 2;
    }

    protected int getSubClassRealSize() {
        return mDatas.size();
    }

    public abstract ViewHolder onCreate(ViewGroup parent, final int viewType);

    public abstract void onBind(ViewHolder viewHolder, int RealPosition, T data);

    public class Holder extends ViewHolder {
      public Holder(View itemView) {
        super(itemView);
      }
    }
    
    public interface OnItemClickListener<T> {
      void onItemClick(View v, int position, T data);
    }

    public interface OnItemLongClickListener<T> {
        void onItemLongClick(View v, int position, T data);
    }


    /**
     * 销毁时动作
     */
    public void destoryAction(){

    }
  }